package com.mmall.util;

import com.mmall.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class JsonUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        //对象的所有字段全部列入
        objectMapper.setSerializationInclusion(Inclusion.ALWAYS);

        //取消默认转换timestamps形式
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);

        //忽略空bean转json的错误
        objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false);

        //所有的日期格式统一为以下的样式。即yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat(DateTimeUtil.STANDARD_FORMAT));

        //忽略 在json字符串种存在。但在java对象种不存在的对应属性。防止错误
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);

    }

    public static <T> String obj2String(T obj){
         if (obj == null){
             return null;
         }
        try {
            return obj instanceof String?(String)obj : objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
             log.warn("Parse Object to String error e");
            return null;
        }
    }


    //返回格式化好的json  <T> 申明方法持有一个类型T 或者说将此方法申明为泛型方法
    public static <T> String obj2StringPretty(T obj){
        if (obj == null){
            return null;
        }

        try {
            return obj instanceof String?(String)obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (Exception e) {
            log.warn("Parse Object to String error e");
            return null;
        }
    }


    //字符串转对象

    public  static <T> T string20bj(String str,Class<T> clazz){
        if (StringUtils.isEmpty(str) || clazz ==null){
            return null;
        }

        try {
            return clazz.equals(String.class)?(T) str : objectMapper.readValue(str,clazz);
        } catch (Exception e) {
            log.warn("Parse String to Object error e");
            return null;
        }
    }

    public  static <T> T string20bj(String str,TypeReference<T> tTypeReference){
        if (StringUtils.isEmpty(str) || tTypeReference ==null){
            return null;
        }

        try {
            return (T) (tTypeReference.getType().equals(String.class)?str : objectMapper.readValue(str,tTypeReference));
        } catch (Exception e) {
            log.warn("Parse String to Object error e",e);
            return null;
        }

    }



    public  static <T> T string20bj(String str,Class<?> collectionClass,Class<?>... elementClasses){
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);

        try {
            return objectMapper.readValue(str,javaType);
        } catch (Exception e) {
            log.warn("Parse String to Object error e",e);
            return null;
        }

    }

    public static void main(String[] args) {
        User u1 = new User();
        u1.setId(1);
        u1.setEmail("541867858@qq.com");

        User u2 = new User();
        u2.setId(2);
        u2.setEmail("541867858@qq.com");
        List<User> list = new ArrayList<>();
        list.add(u1);
        list.add(u2);

        String user1Json = JsonUtil.obj2String(list);

        String user1JsonPretty = JsonUtil.obj2StringPretty(list);

        List<User> list2 = JsonUtil.string20bj(user1Json, new TypeReference<List<User>>() {
        });

        log.info("user1Json:{}",user1Json);

        log.info("user1JsonPretty:{}",user1JsonPretty);

        log.info("list2:{}"+list2);
    }

}

